package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.SysHouse;
import com.ruoyi.system.service.ISysHouseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房间表Controller
 * 
 * @author ruoyi
 * @date 2021-08-16
 */
@RestController
@RequestMapping("/system/house")
public class SysHouseController extends BaseController
{
    @Autowired
    private ISysHouseService sysHouseService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询房间表列表
     */
    @PreAuthorize("@ss.hasPermi('system:house:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysHouse sysHouse)
    {
        startPage();
        if (sysHouse.getDeptId() == null) {
            sysHouse.setCommunityId(SecurityUtils.getCommunityId());
        }
        List<SysHouse> list = sysHouseService.selectSysHouseList(sysHouse);
        return getDataTable(list);
    }

    // 户主查询自己在登录社区的房产
    @GetMapping("/user")
    public AjaxResult listByUserId()
    {
        List<SysHouse> houses = sysHouseService.listByUserId(SecurityUtils.getUserID(), SecurityUtils.getCommunityId());
        return AjaxResult.success(houses);
    }
    /**
     * 导出房间表列表
     */
    @PreAuthorize("@ss.hasPermi('system:house:export')")
    @Log(title = "房间表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysHouse sysHouse)
    {
        List<SysHouse> list = sysHouseService.selectSysHouseList(sysHouse);
        ExcelUtil<SysHouse> util = new ExcelUtil<SysHouse>(SysHouse.class);
        return util.exportExcel(list, "房间表数据");
    }

    /**
     * 获取房间表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:house:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(sysHouseService.selectSysHouseById(id));
    }

    /**
     * 新增房间表
     */
    @PreAuthorize("@ss.hasPermi('system:house:add')")
    @Log(title = "房间表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysHouse sysHouse)
    {
        // 创建
        SysDept dept = new SysDept();
        dept.setParentId(sysHouse.getDeptId());
        dept.setDeptName(sysHouse.getName());

        if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
        {
            return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }

        dept.setType("3");
        dept.setOrderNum("1");
        // 添加 house 相关属性
        dept.setSysHouse(sysHouse);


        return toAjax(deptService.insertDept(dept));
    }

    /**
     * 修改房间表
     */
    @PreAuthorize("@ss.hasPermi('system:house:edit')")
    @Log(title = "房间表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysHouse sysHouse)
    {
        return toAjax(sysHouseService.updateSysHouse(sysHouse));
    }

    /**
     * 删除房间表
     */
    @PreAuthorize("@ss.hasPermi('system:house:remove')")
    @Log(title = "房间表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        if (sysHouseService.checkHouseExistUser(ids))
        {
            return AjaxResult.error("请将房间用户清空在删除");
        }
        return toAjax(sysHouseService.deleteSysHouseByIds(ids));
    }

    /**
     * 根据房屋 Id 查询成员
     * @param id
     * @return
     */
    @GetMapping("/member/{deptId}")
    public AjaxResult getMember(@PathVariable Integer deptId) {
        List<SysUser> users = sysHouseService.selectMember(deptId);
        return AjaxResult.success(users);
    }

    @DeleteMapping("/member/{userId}/{deptId}")
    public AjaxResult rmMember(@PathVariable Long userId, @PathVariable Long deptId) {
        return toAjax(sysHouseService.rmMember(userId, deptId));
    }
}
